package com.lei.sqlGoodsJsp.servlet;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lei.sqlGoodsJsp.constant.UserConstant;

public class SessionLognServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getParameter("method");
		if("login".equals(method)) {
			login(req,resp);
		}else if("loginOut".equals(method)) {
			loginOut(req,resp);
		}
		
	}
	protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String userName =req.getParameter("userName");
		
		if ("admin".equals(userName) && "123".equals(req.getParameter("password"))) {
			//登陆成功
			req.getSession().setAttribute(UserConstant.USER_INFO, userName);
			resp.sendRedirect("GoodsPageServlet?method=list");
		} else {
			req.setAttribute(UserConstant.ERROR_MSG, "用户名或密码错误 ");
			req.getRequestDispatcher("sql/login.jsp").forward(req, resp);
		}
	}
	protected void loginOut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("退出");
		req.getSession().invalidate();
		resp.sendRedirect("sql/login.jsp");
		
	}
}
